/**
 * Created by guoshuai on 2016/11/19.
 */
toastr.options = {
    positionClass: 'toast-bottom-right'
};
jconfirm.defaults = {
    title: '提示',
    type: 'warning',
    buttons: {}
};
var toast = {
    success: function (msg) {
        toastr.success(msg, 'Success');
    },
    error: function (error) {
        toastr.error(error, 'Failure');
    }
};
var confirmDialog = {
    drop: function (id, name, clazz) {
        $.confirm({
            content: '您确定要删除【' + name + '】吗？',
            buttons: {
                deleteAction: {
                    text: '确定',
                    action: function () {
                        WaitingDialog.show();
                        request.deleteEntity("resources/entity", clazz, id,
                            function (data) {
                                WaitingDialog.dismiss();
                                toast.success('删除成功');
                                defaultCallback();
                            }, function (data) {
                                WaitingDialog.dismiss();
                                toast.error('删除失败：' + JSON.stringify(data));
                            });
                    }
                },
                cancelAction: {
                    text: '取消'
                }
            }
        });
    }
};
var WaitingDialog = {
    show: function () {
        var dialog = $('<div class="loading"><span></span> <span></span> <span></span> <span></span> <span></span></div>');
        dialog.appendTo('body');
        var top = ($(window).height() - dialog.height()) / 2;
        var left = ($(window).width() - dialog.width()) / 2;
        var scrollTop = $(document).scrollTop();
        var scrollLeft = $(document).scrollLeft();
        dialog.css({'top': top + scrollTop, left: left + scrollLeft}).show();
    },
    dismiss: function () {
        $(".loading").remove();
    }
};

$("#ocean-modal").on("hidden", function (e) {
    $(this).removeData("modal");
});
var oceanModal = {
    openDefault: function (url) {
        $("#ocean-modal-dialog-div").load(url, function () {
            $("#ocean-modal").modal();
        });
    },
    open: function (id, url) {
        $("#ocean-modal-dialog-div").load(url, function () {
            $("#" + id).modal();
        });
    },
    closeDefault: function () {
        $("#ocean-modal").modal('hide');
    },
    close: function (id) {
        $("#" + id).modal('hide');
    }
};

function formSubmit(validator, callback) {
    if (validator != null && !validator.form()) {
        return;
    }
    if (callback == null) {
        callback = defaultCallback;
    }

    var form = $('#ocean-modal form');
    var url = form.data("url")
    var obj = form.data("class");

    console.log(url);
    console.log(form.serialize());

    if (obj == "") {
        WaitingDialog.show();
        request.post(url, form.serialize(), function () {
            WaitingDialog.dismiss();
            toast.success('提交成功');
            callback();
        }, function (error) {
            WaitingDialog.dismiss();
            toast.error('提交失败：' + JSON.stringify(error));
        });
    } else {
        var json = $utils.parse_query(form.serialize());
        console.log(obj);
        console.log(json);
        var id = $("#entity_id").val();
        if (id == "" || id == null) {
            WaitingDialog.show();
            request.postEntity(url, obj, JSON.stringify(json),
                function (data) {
                    WaitingDialog.dismiss();
                    toast.success('提交成功');
                    callback(data);
                }, function (data) {
                    WaitingDialog.dismiss();
                    toast.error('提交失败：' + JSON.stringify(data));
                });
        } else {
            WaitingDialog.show();
            request.setEntity(url, obj, id, JSON.stringify(json),
                function (data) {
                    WaitingDialog.dismiss();
                    toast.success('提交成功');
                    callback(data);
                }, function (data) {
                    WaitingDialog.dismiss();
                    toast.error('提交失败：' + JSON.stringify(data));
                });
        }
    }
}

var defaultCallback = function (data) {
    oceanModal.closeDefault();
    load();
};